#include <bits/stdc++.h>
using namespace std;
const int N = 205;
class Solution
{
    int dx[4] = { -1,1,0,0 };
    int dy[4] = { 0,0,-1,1 };
    bool ping[N][N];
    bool xi[N][N];
    int n, m;
public:

    vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights)
    {
        n = heights.size();
        m = heights[0].size();
        for (int i = 0; i < m; i++)
        {

            //太平
            ping[0][i] = true;
            dfs(0, i, heights, ping);
            //大西
            xi[n - 1][i] = true;
            dfs(n - 1, i, heights, xi);
        }
        for (int i = 0; i < n; i++)
        {
            ping[i][0] = true;
            dfs(i, 0, heights, ping);
            xi[i][m - 1] = true;
            dfs(i, m - 1, heights, xi);
        }
        vector<vector<int>> ret;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (ping[i][j] && xi[i][j])
                {
                    ret.push_back({ i,j });
                }
            }
        }
        return ret;

    }
    void dfs(int i, int j, vector<vector<int>>& heights, bool (*check)[N])
    {
        for (int k = 0; k < 4; k++)
        {
            int x = dx[k] + i;
            int y = dy[k] + j;
            if (x >= 0 && x < n && y >= 0 && y < m && !check[x][y] && heights[i][j] <= heights[x][y])
            {
                check[x][y] = true;
                dfs(x, y, heights, check);
            }
        }
    }
};


